//http://excid3.com/blog/2010/06/android-requires-class-compatibility-set-to-5-0/
//http://developer.android.com/guide/tutorials/views/hello-mapview.html
//http://mobiforge.com/developing/story/using-google-maps-android
//http://code.google.com/apis/maps/documentation/javascript/maptypes.html#StyledMaps

package android.theHead;

import java.util.Hashtable;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ViewMap extends MapActivity {

	MapView mapView;
	MapController mapController;
	List<Overlay> mapOverlays;
	Drawable drawable;
	Hashtable<Integer, OverlayItem> boats = new Hashtable<Integer, OverlayItem>();

	TeamDBAdapter teamDB;
	Cursor c;
	public HeadChangeReceiver receiver = null;
	MapItemizedOverlay boatsOverlay;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// starting the background service
		Log.i("theHead","Starting background service");
		this.startService(new Intent(this, BackgroundService.class));

		// make an instance of the teamDBAdatpter to update the team info if necessary 
		teamDB = new TeamDBAdapter(this);
		teamDB.open();
		Log.i("theHead","updating team info");
		teamDB.updateTeamInfo();
		
		setContentView(R.layout.map);

		c = teamDB.getAllInfo();
		
		// define map
		mapView = (MapView) findViewById(R.id.mapview);
		mapView.setBuiltInZoomControls(true);
		//     mapView.setSatellite(true);

		// define map controls
		mapController = mapView.getController();
		mapController.setZoom(13);
		mapController.animateTo(new GeoPoint(52326842, 4905567));

		// define map overlays
		mapOverlays = mapView.getOverlays();

		addBoats();
		
		// initial boats creation
//		changeBoats();
	}

	public void onResume(){
		super.onResume();

		IntentFilter filter;
		filter = new IntentFilter(BackgroundService.NEW_HEAD_DATA);
		receiver = new HeadChangeReceiver();
		registerReceiver(receiver, filter);
	}
	
	public void onDestroy(){
		super.onDestroy();

		// stop the background service
		Log.i("theHead","stopping background service");
		this.stopService(new Intent(this, BackgroundService.class));

		Log.i("ListView", "onDestroy");
		c.close();
		teamDB.close();
	
		unregisterReceiver(receiver);
		
		
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void addBoats() {
		Drawable drawable = getResources().getDrawable(R.drawable.sko);
		boatsOverlay = new MapItemizedOverlay(drawable, this);
		
		
		// drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
		
		while(c.moveToNext()){
			boatsOverlay = addMarker(c, boatsOverlay);
		}
		mapOverlays.add(boatsOverlay);
	}
	
	public MapItemizedOverlay addMarker(Cursor c, MapItemizedOverlay boatsOverlay){
		int longitude = (int)(c.getDouble(c.getColumnIndex("longitude"))*1000000);
		int latitude = (int)(c.getDouble(c.getColumnIndex("latitude"))*1000000);
		String name  = c.getString(c.getColumnIndex("teamName"));
		String snippet= "coaches: " + c.getString(c.getColumnIndex("coaches"));
		
		HeadOverlayItem overlayitem = new HeadOverlayItem(new GeoPoint(latitude, longitude), name, snippet, c.getInt(c.getColumnIndex("teamID")));
		
		drawable = getResources().getDrawable(getResources().getIdentifier(c.getString(c.getColumnIndex("clubID")).toLowerCase(), "drawable", "android.theHead"));
		drawable.setBounds(-drawable.getIntrinsicWidth()/4, -drawable.getIntrinsicHeight()/4, drawable.getIntrinsicWidth()/4, drawable.getIntrinsicHeight()/4);
//		drawable.set
		boats.put(c.getInt(c.getColumnIndex("teamID")), overlayitem);
		overlayitem.setMarker(drawable);

		boatsOverlay.addOverlay(overlayitem);
		return boatsOverlay;
	}

	public void changeBoats(Cursor c){
		while(c.moveToNext()){
			int teamID = c.getInt(c.getColumnIndex("teamID"));
			if (boats.containsKey(teamID)){
//				((OverlayItem) boats.get(teamID)).getPoint()
			}
		}
	}


	public class HeadChangeReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getIntExtra(BackgroundService.HEAD_DATA_CHANGED, 0)== BackgroundService.READINGS_TABLE_UPDATED){
				Log.i("ListView", "ReadingsTable changed, updating!");
				c.requery();
				mapView.getOverlays().clear();
				mapView.invalidate();
//				boatsOverlay.clear();
				Log.i("ViewMap", "redraw");
				addBoats();


//				changeBoats(c);
			}
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.spectator_menu, menu);
		menu.removeItem(R.id.gotoMapView);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Context thisContext = this;
		Intent intent;

		// Handle item selection
		switch (item.getItemId()) {
		case R.id.gotoMapView:
			intent = new Intent(thisContext, ViewMap.class);
			thisContext.startActivity(intent);
			return true;
		case R.id.gotoRowMode:
			intent = new Intent(thisContext, RowmodeIntro.class);
			thisContext.startActivity(intent);
			return true;
		case R.id.gotoListView:
			intent = new Intent(thisContext, Overview.class);
			thisContext.startActivity(intent);
			return true;
		case R.id.gotoQuit:
			finish();	
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
